<%@ page language = "java" import = "java.util.*" %>
<%@ page import = "edu.colorado.karl.manctrl.ManualControl" %>

<link rel="Stylesheet" type="text/css" media="Screen" charset="utf-8" href="css/common.css"/>

<%@ include file = "header.jspf" %>

<h1>Updating Setpoints...</h1>

<%

ManualControl mc = ManualControl.getManualControl();
boolean update = true;

try
{
  //obtain user input from html form
  int roomTemp = Integer.parseInt(request.getParameter("roomTemp"));
  int roomRange = Integer.parseInt(request.getParameter("roomRange"));
  
  int hotTank = Integer.parseInt(request.getParameter("hotTank"));
  //int hotTankRange = Integer.parseInt(request.getParameter("hotTankRange"));
  int coldTank = Integer.parseInt(request.getParameter("coldTank"));
  //int coldTankRange = Integer.parseInt(request.getParameter("coldTankRange"));

  //check the tank mode setting
  String tankMode = request.getParameter("tank_mode");

  //Check the house heating/cooling mode
  String heatingCoolingMode = request.getParameter("hc_mode");
  
  boolean heatingCoolingModeB = false;

  if(heatingCoolingMode != null)
  {
    if(heatingCoolingMode.equals("heating"))
    {
      heatingCoolingModeB = true;
    }
    else
    {
      heatingCoolingModeB = false;
    }
  }

  //margin of error must be set between 1 and 10, inclusive
  if(roomRange < 1 || roomRange > 10)
  {
    update = false;

    %>

    <br>Error, range must be 1 - 10.
    <br>redirecting to manual control page in 2 seconds...
    <meta http-equiv=REFRESH content=2;url=manual_control.jsp>

    <%
  }

  if(roomTemp < 50 || roomTemp > 100)
  {
    update = false;

    %>

    <br>Error, room temperature must be 50-100.
    <br>redirecting to manual control page in 2 seconds...
    <meta http-equiv=REFRESH content=2;url=manual_control.jsp>


    <%    
  }

  if(update)
  {
    mc.updateRoomSettings(roomTemp,roomRange);
  }

  //Now update the tank setpoints...
  update = true;

  if(hotTank < 80 || hotTank > 150)
  {
    update = false;
    %>

    <br>Error, hot tank temperature must be 80 - 150.
    <br>redirecting to manual control page in 2 seconds...
    <meta http-equiv=REFRESH content=2;url=manual_control.jsp>


    <%
  }

  if(coldTank < 0 || coldTank > 30)
  {
    update = false;
    %>

    <br>Error, cold tank temperature must be 0 - 30.
    <br>redirecting to manual control page in 2 seconds...
    <meta http-equiv=REFRESH content=2;url=manual_control.jsp>


    <%
  }

  //update the database entries
  boolean tankModeB = false;
  //set the new tank mode (if needed)
  if(tankMode != null)
  {
    if(tankMode.compareTo("tank_auto") == 0)
    {
      tankModeB = true;
    }

    else
    {
      tankModeB = false;
    }
  }


  if(update)
  {
    mc.updateTankSettings(hotTank,5,coldTank,5,tankModeB,heatingCoolingModeB);
  }


%>

  <br>Changes saved successfully.
  <br>redirecting to manual control page in 2 seconds...
  <meta http-equiv=REFRESH content=2;url=manual_control.jsp>


<%

}

//the user most likely entered a non-numerical or decimal value
catch(NumberFormatException e)
{
%>

  <br>Error, all inputs must be integers.
  <br>redirecting to manual control page in 2 seconds...
  <meta http-equiv=REFRESH content=2;url=manual_control.jsp>

<%
}

%>

</body>
</html>